<template>
  <div class="my-container">
    <!-- 未登录 -->
    <div class="header not-login" v-if="!user">
      <img src="~@/assets/mobile.png" @click="$router.push('/login')" alt="" />
      <span>登录 / 注册</span>
    </div>
    <!-- 已登录 -->
    <div class="header login" v-else>
      <div class="top">
        <div class="left">
          <van-image round :src="userInfo.photo" />
          <span>{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button round to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <div class="bottom">
        <div class="item">
          <span>{{ userInfo.art_count }}</span>
          <span>头条</span>
        </div>
        <div class="item">
          <span>{{ userInfo.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="item">
          <span>{{ userInfo.fans_count }}</span>
          <span>粉丝</span>
        </div>
        <div class="item">
          <span>{{ userInfo.like_count }}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>

    <van-grid :column-num="2" class="page-grid">
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text">历史</span>
      </van-grid-item>
    </van-grid>

    <van-cell-group class="cell1">
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
    </van-cell-group>

    <van-cell-group v-if="user">
      <van-cell title="退出登录" class="logout" @click="logout" />
    </van-cell-group>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user'
export default {
  name: 'MyPage',
  components: {},
  props: {},
  data () {
    return {
      userInfo: {} // 用户信息
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.loadUserInfo()
  },
  mounted () { },
  methods: {
    // 退出
    logout () {
      this.$dialog.confirm({
        title: '确定退出吗？'
      })
        .then(() => {
          // 调用vuex里的outUser退出功能
          this.$store.commit('outUser')
        })
        .catch(() => {
          // on cancel
        })
    },
    async loadUserInfo () {
      try {
        const { data } = await getUserInfo()
        this.userInfo = data.data
        console.log(this.userInfo)
      } catch (err) {
        console.log(err)
        this.$toast('获取数据失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.my-container {
  font-family: MicrosoftYaHei;
  .header {
    height: 361px;
    background: url('~@/assets/banner.png') no-repeat;
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    img {
      width: 132px;
      height: 132px;
      margin-bottom: 16px;
      vertical-align: top;
    }
    span {
      font-size: 28px;
      color: #fff;
    }
  }
  .login {
    .top {
      height: 231px;
      // background-color: pink;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        /deep/ img {
          width: 132px;
          height: 132px;
        }
        span {
          font-size: 30px;
          color: #fff;
          margin-left: 22px;
        }
      }
      .right {
        .van-button {
          width: 116px;
          height: 33px;
          border-radius: 16px;
          font-size: 20px;
          white-space: nowrap;
          color: #666666;
        }
      }
    }
    .bottom {
      height: 130px;
      // background-color: aqua;
      display: flex;
      .item {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        span {
          color: #fff;
          &:first-child {
            font-size: 36px;
          }
          &:last-child {
            font-size: 23px;
          }
        }
      }
    }
  }
  // 收藏和历史样式
  .page-grid .van-grid-item {
    &:first-child {
      .toutiao {
        font-size: 45px;
        color: #eb5253;
      }
      span {
        font-size: 28px;
      }
    }
    &:last-child {
      .toutiao {
        font-size: 45px;
        color: #ff9d1d;
      }
      span {
        font-size: 28px;
      }
    }
  }
  .cell1 {
    margin: 9px 0;
    .van-cell {
      font-size: 30px;
    }
  }
  .logout {
    text-align: center;
    color: #d86262;
    font-size: 30px;
  }
}
</style>
